//
//  KLProgressHUD+Tips.m
//  HealthManageDoctor
//
//  Created by 23 on 2017/9/11.
//  Copyright © 2017年 kinglian. All rights reserved.
//

#import "KLProgressHUD+Tips.h"
#define kCustomHUDTag 22222

@implementation KLProgressHUD (Tips)

+ (instancetype)showTipsInView:(UIView *)superView WithTitle:(NSString *)title buttonTitle:(NSString *)buttonTitle target:(id)target selector:(SEL)selector time:(double)time isBackEnable:(BOOL)isBackEnable
{
    if (superView == nil) {
        superView = [UIApplication sharedApplication].keyWindow;
    }
    
    
    //主视图
    UIView *view = [[UIView alloc] init];
    view.tag = kCustomHUDTag;
    view.backgroundColor = kRGBColor(253, 185, 141);
    
    //按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:buttonTitle forState:UIControlStateNormal];
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor whiteColor];
    button.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [button setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [view addSubview:button];
    [button sizeToFit];
    button.app_width += 20;
    
    //label
    UILabel *label = [[UILabel alloc] init];
    label.text = title;
    label.numberOfLines = 0;
    label.preferredMaxLayoutWidth = kScreenWidth - 20 - 20 - button.app_width;
    [view addSubview:label];
    
    
    //调价约束
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view);
        make.left.mas_equalTo(view.mas_left).offset(20);
        make.right.mas_equalTo(button.mas_left).offset(-20);
    }];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view);
        make.right.mas_equalTo(view.mas_right).offset(-20);
    }];
    
    view.app_width = kScreenWidth;
    [view layoutIfNeeded];
    CGFloat height = label.bounds.size.height + 20 + 20;
    
    view.frame = CGRectMake(0, 0, kScreenWidth, height);
    
    
    KLProgressHUD *hud = [[KLProgressHUD alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, height)];
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = view;
    hud.color = [UIColor redColor];
    hud.margin = 0;
    hud.cornerRadius = 0;
    hud.userInteractionEnabled = !isBackEnable;
    
    hud.removeFromSuperViewOnHide = YES;
    [superView addSubview:hud];
    [hud show:YES];
    if (time == 0) {
        time = 1.5;//默认的
    }
    [hud hide:YES afterDelay:time];
    return hud;
}


+ (UIView *)showCustomTipsInView:(UIView *)superView WithTitle:(NSString *)title buttonTitle:(NSString *)buttonTitle target:(id)target selector:(SEL)selector time:(double)time showAnimated:(BOOL)showAnimated hideAnimated:(BOOL)hideAnimated
{
    if (superView == nil) {
        superView = [UIApplication sharedApplication].keyWindow;
    }
    
    
    //主视图
    UIView *view = [[UIView alloc] init];
    view.tag = kCustomHUDTag;
    view.backgroundColor = kRGBColor(253, 185, 141);
    
    //按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:buttonTitle forState:UIControlStateNormal];
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor whiteColor];
    button.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [view addSubview:button];
    [button sizeToFit];
    button.app_width += 20;
    CGFloat btnWidth = button.app_width;
    
    //label
    UILabel *label = [[UILabel alloc] init];
    label.text = title;
    label.numberOfLines = 0;
    label.preferredMaxLayoutWidth = kScreenWidth - 20 - 20 - button.app_width;
    [view addSubview:label];
    
    
    //调价约束
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view);
        make.left.mas_equalTo(view.mas_left).offset(20);
        make.right.mas_equalTo(button.mas_left).offset(-20);
    }];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view);
        make.right.mas_equalTo(view.mas_right).offset(-20);
        make.width.mas_equalTo(btnWidth);
    }];
    
    view.app_width = kScreenWidth;
    [view layoutIfNeeded];
    if (label.app_width < label.preferredMaxLayoutWidth) {
        label.app_width = label.preferredMaxLayoutWidth;
    }
    CGFloat height = label.bounds.size.height + 20 + 20;
    
    view.bounds = CGRectMake(0, 0, kScreenWidth, height);
    view.center = superView.center;
    view.alpha = 0.01;
    [superView addSubview:view];
    NSTimeInterval show = showAnimated?0.25:0.0;
    [UIView animateWithDuration:show animations:^{
        view.alpha = 1.0;
    }];

    
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    info[@"kAnimated"] = @(hideAnimated);
    info[@"kView"] = view;
    NSTimer *timer = [NSTimer timerWithTimeInterval:time target:self selector:@selector(hideAnimaion:) userInfo:info repeats:NO];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
    return view;
}


#pragma mark - 工具方法
/**隐藏所有的视图*/
+ (void)dismissAllCustomHUD
{
    for (UIView *view in [UIApplication sharedApplication].keyWindow.subviews) {
        if (view.tag == kCustomHUDTag) {
            [view removeFromSuperview];
        }
    }
}

/**定时器动画隐藏*/
+ (void)hideAnimaion:(NSTimer *)timer
{
    NSDictionary *info = timer.userInfo;
    BOOL hideAnimated = [info[@"kAnimated"] boolValue];
    UIView *view = info[@"kView"];
    NSTimeInterval hide = hideAnimated?0.25:0.0;
    [UIView animateWithDuration:hide animations:^{
        view.alpha = 0.01;
    } completion:^(BOOL finished) {
        [view removeFromSuperview];
    }];
}


@end
